package com.jhs.kjs.model.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

public abstract class AFileManager {
	private static final Logger logger = LoggerFactory.getLogger(AFileManager.class);
	protected String fileName;
	protected MultipartFile file;
	protected HttpServletRequest req;

	abstract protected String getContextDownloadPath();

	abstract protected String getContextUploadPath();

	abstract public String getDownloadFilePath();
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jhs.kjs.model.file.IFileManager#loadFile(java.lang.String)
	 */
	public boolean loadFile(String fileName) {
		File filePath = new File(getContextDownloadPath());
		if (!filePath.exists())
			filePath.mkdir();
		File loadfile = new File(getContextDownloadPath() + File.separator
				+ fileName);
		if (!loadfile.exists()) {
			try {
				File uploadedFilePath = new File(getContextUploadPath()
						+ File.separator + fileName);
				// uploadedFilePath.renameTo(loadfile);
				fileCopy(uploadedFilePath.getAbsolutePath(),
						loadfile.getAbsolutePath());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return loadfile.isFile();
	}
	
	public boolean loadFile(String fileName, String originalFileName) {
		File filePath = new File(getContextDownloadPath());
		if (!filePath.exists())
			filePath.mkdir();
		File loadfile = new File(getContextDownloadPath() + File.separator
				+ originalFileName);
		if (!loadfile.exists()) {
			try {
				File uploadedFilePath = new File(getContextUploadPath()
						+ File.separator + fileName);
				// uploadedFilePath.renameTo(loadfile);
				fileCopy(uploadedFilePath.getAbsolutePath(),
						loadfile.getAbsolutePath());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return loadfile.isFile();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.jhs.kjs.model.file.IFileManager#saveFile()
	 */
	public boolean saveFile() {
		if (this.file == null || this.req == null)
			throw new NullPointerException();
		if (!this.file.isEmpty()) {

			File filePath = new File(getContextUploadPath());
			if (!filePath.exists())
				filePath.mkdir();

			File file = new File(getContextUploadPath() + File.separator
					+ makeFileName(this.file.getOriginalFilename()));
			try {
				this.file.transferTo(file);
			} catch (IllegalStateException e) {
				return false;
			} catch (IOException e) {
				return false;
			}
		}
		return true;
	}

	// 파일을 복사하는 메소드
	protected static void fileCopy(String inFileName, String outFileName) {
		try {
			FileInputStream fis = new FileInputStream(inFileName);
			FileOutputStream fos = new FileOutputStream(outFileName);

			int data = 0;
			while ((data = fis.read()) != -1) {
				fos.write(data);
			}
			fis.close();
			fos.close();

		} catch (IOException e) {
			logger.debug("파일을 찾지 못 했습니다.");
		}
	}

	protected String makeFileName(String fileName) {
		String ret = "" + Calendar.getInstance().getTimeInMillis();
		Random r = new Random();
		ret += r.nextInt(10000000) + ".";
		String[] fn = fileName.split("[.]");
		if(fn.length>1){
			ret+=fn[fn.length-1];
		}
		setFileName(ret);
		return getFileName();
	}

	protected void setFileName(String fileName) {
		this.fileName = fileName;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jhs.kjs.model.file.IFileManager#setFile(org.springframework.web.multipart
	 * .MultipartFile)
	 */
	public void setFile(MultipartFile file) {
		this.file = file;
	}

	public String getFileName() {
		return this.fileName;
	}
	public String getOriginalFilename(){
		return this.file.getOriginalFilename();
	};
	
	
}
